import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { FetchService } from '../../../core/net/fetch.service';

@Component({
  selector: 'app-sf-danger',
  templateUrl: './sf-danger.component.html',
  styleUrls: ['./sf-danger.component.scss']
})
export class SfDangerComponent implements OnInit {
    searchForm: FormGroup;
    tableData: Array<any> = [];
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = false;
    form: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;
    queryType: "";

    constructor(private fb: FormBuilder,
                private message: NzMessageService,
                private fetchService: FetchService) {
        this.searchForm = this.fb.group({
            level: [''],
            type: [''],
            statu: [''],
            project: [''],
            rangePickerTime: [[]]
        });
        this.form = this.fb.group({
            id: [null],
            name: ['', Validators.required],
            level: ['', Validators.required],
            type: ['', Validators.required],
            statu: ['', Validators.required],
            project: ['', Validators.required],
            investigateDate: ['', Validators.required],
            changeDate: ['', Validators.required],
            changeStatu: ['', Validators.required],
            reportDept: ['', Validators.required],
            remark: ['', Validators.required]
        });
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    queryInfo(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.loading = true;
        const params = {
            stTime: this.searchForm.value.rangePickerTime[0],
            edTime: this.searchForm.value.rangePickerTime[1],
            level: this.searchForm.value.level,
            type: this.searchForm.value.type,
            statu: this.searchForm.value.statu,
            project: this.searchForm.value.project,
            pageSize: this.pageSize,
            pageNum: this.pageNum
        };
        this.fetchService.post('safeProduct/wrsSafeProductHidden/list', params).subscribe(res => {
            this.tableData = res['list'];
            this.total = res['total'];
            this.loading = false;
            // this.fileList = [];
        });
    }

    openEditModal(isEditable: boolean = true, item?): void {
        this.modalTitle = isEditable ? (item ? `编辑` : '新增') : '详情';
        this.queryType = isEditable ? "" : null;

        this.form.patchValue(item || {});
        if (isEditable) {
            this.form.enable();
        } else {
            this.form.disable();
        }
        this.modalShow = true;
    }

    onDelete(id) {
        this.fetchService.get(`safeProduct/wrsSafeProductHidden/remove/${id}`).subscribe(resp => {
            this.message.info('删除成功');
            this.queryInfo();
        });
    }

    onConfirm() {
        if (!this.form.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = this.form.value;
        const url = data.id ? 'safeProduct/wrsSafeProductHidden/edit' : 'safeProduct/wrsSafeProductHidden/add';
        this.fetchService.post(url, data).subscribe(res => {
            this.message.success('提交成功');
            this.modalShow = false;
            this.queryInfo();
            this.form.reset();
        });
    }

    onCancel() {
        this.form.reset();
        this.modalShow = false;
    }
}
